import { h } from 'vue';
import { Tag } from 'ant-design-vue';
import { BasicColumn, FormSchema } from '@/components/Table';
import { formatToDateTime } from '@/utils/dateUtil';
import { listDeptApi } from '@/api/system/dept';

export const columns: BasicColumn[] = [
  {
    title: '部门名称',
    dataIndex: 'name',
    // width: 180,
  },
  {
    title: '排序',
    dataIndex: 'deptSort',
    // width: 180,
  },
  {
    title: '状态',
    dataIndex: 'deptStatus',
    customRender: ({ record }) => {
      const status = record.deptStatus;
      const enable = status === '0' ? true : false;
      const color = enable ? 'green' : 'red';
      const text = enable ? '正常' : '禁用';
      return h(Tag, { color: color }, () => text);
    },
  },
  {
    title: '创建时间',
    dataIndex: 'createTime',
    width: 180,
    customRender: ({ text }) => formatToDateTime(text),
  },
];

export const searchFormSchema: FormSchema[] = [
  {
    field: 'name',
    label: '部门名称',
    component: 'Input',
    colProps: { span: 4 },
  },
  {
    field: 'deptStatus',
    label: '状态',
    component: 'Select',
    componentProps: {
      options: [
        { label: '正常', value: '0' },
        { label: '禁用', value: '1' },
      ],
    },
    colProps: { span: 4 },
  },
];

export const formSchema: FormSchema[] = [
  {
    field: 'parentId',
    label: '上级部门',
    component: 'ApiTreeSelect',
    // parentid是0说明就是根节点的了 根节点不能改parentid
    ifShow: ({ model }) => model.parentId !== '0',
    componentProps: {
      api: listDeptApi,
      // fieldNames: {
      //   label: 'name',
      //   key: 'id',
      //   value: 'id',
      // },
      labelField: 'name',
      valueField: 'fullAncestors',
      immediate: true,

      // getPopupContainer: () => document.body,
    },
  },
  {
    field: 'name',
    label: '部门名称',
    required: true,
    component: 'Input',
    componentProps: {
      maxlength: 50,
      showCount: true,
    },
    rules: [{ min: 2, max: 50, message: '部门名称长度必须介于 2 和 50 之间', trigger: 'blur' }],
  },
  {
    field: 'deptSort',
    label: '显示排序',
    component: 'InputNumber',
    defaultValue: 0,
    componentProps: {
      min: 0,
      precision: 0,
    },
    required: true,
  },
  {
    field: 'phone',
    label: '联系电话',
    component: 'Input',
    componentProps: {
      maxlength: 11,
      showCount: true,
    },
    rules: [
      { pattern: /^1[3|4|5|6|7|8|9][0-9]\d{8}$/, message: '请输入正确的手机号码', trigger: 'blur' },
    ],
  },
  {
    field: 'email',
    label: '邮箱',
    component: 'Input',
    componentProps: {
      maxlength: 300,
      showCount: true,
    },
    rules: [{ type: 'email', message: '请输入正确的邮箱地址', trigger: 'blur' }],
  },
  {
    field: 'deptStatus',
    label: '部门状态',
    component: 'RadioGroup',
    defaultValue: '0',
    componentProps: {
      options: [
        { label: '正常', value: '0' },
        { label: '禁用', value: '1' },
      ],
    },
  },
];
